package com.sxm.yiwei.pinche;

import android.app.ProgressDialog;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.squareup.picasso.Picasso;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.MineMember;
import com.sxm.yiwei.base.BaseActivity;
import com.sxm.yiwei.beans.pinche.PassengerOrder;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.views.CenterDialog;

import org.xutils.x;


public class PassengerOrderDetailActivity extends BaseActivity implements View.OnClickListener, CenterDialog.OnCenterItemClickListener {

    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_style;
    private TextView tv_distance;
    private TextView text11;
    private TextView text21;
    private TextView tv_depart_time;
    private TextView tv_start_pos;
    private TextView tv_end_pos;
    private TextView text3;
    private TextView tv_phone;
    private TextView tv_words;
    private ImageView iv_icon;
    private TextView tv_nickname;
    private TextView tv_recent_come;
    private TextView tv_wei;
    private TextView tv_pay_break_money;
    private TextView tv_rob;
    private int type;
    private TextView text1;
    private TextView text2;
    private ProgressDialog progressDialog;
    private CenterDialog centerDialog;
    private int failure_code;
    private String serviceId;//服务主键
    private String ownerId;//车主主键
    private String passerId;//乘客主键
    private String pinLoseAccount;//爽约金
    private PassengerOrder passengerOrder;
    private TextView tv_whos_phone;
    private TextView tv_whos_words;
    //private String order_id="";
    //private String order_code;


    @Override
    public void setContentView() {
        setContentView(R.layout.activity_order_detail_passenger);
    }

    @Override
    public void initView() {
        rl_back = (RelativeLayout)findViewById(R.id.rl_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        tv_style = (TextView)findViewById(R.id.tv_style);
        tv_distance = (TextView)findViewById(R.id.tv_distance);
        text11 = (TextView)findViewById(R.id.text11);
        text21 = (TextView)findViewById(R.id.text21);
        tv_depart_time = (TextView)findViewById(R.id.tv_depart_time);
        tv_start_pos = (TextView)findViewById(R.id.tv_start_pos);
        tv_end_pos = (TextView)findViewById(R.id.tv_end_pos);
        text3 = (TextView)findViewById(R.id.text3);
        text1 = (TextView)findViewById(R.id.text1);
        text2 = (TextView)findViewById(R.id.text2);
        tv_phone = (TextView)findViewById(R.id.tv_phone);
        tv_words = (TextView)findViewById(R.id.tv_words);
        iv_icon = (ImageView)findViewById(R.id.iv_icon);
        tv_nickname = (TextView)findViewById(R.id.tv_nickname);
        tv_recent_come = (TextView)findViewById(R.id.tv_recent_come);
        tv_wei = (TextView)findViewById(R.id.tv_wei);
        tv_pay_break_money = (TextView)findViewById(R.id.tv_pay_break_money);
        tv_rob = (TextView)findViewById(R.id.tv_rob);
        tv_whos_phone = (TextView)findViewById(R.id.tv_whos_phone);
        tv_whos_words = (TextView)findViewById(R.id.tv_whos_words);
    }

    @Override
    public void initListener() {
        rl_back.setOnClickListener(this);
        tv_wei.setOnClickListener(this);
        tv_pay_break_money.setOnClickListener(this);
        tv_rob.setOnClickListener(this);
    }
    private void hostRobFormOrder() {
        JSONObject object = new JSONObject();
        object.put("serviceId", passengerOrder.getService_id());//服务主键
        object.put("ownerId", getHeadInfo().getUserId());//车主主键
        object.put("passerId", passengerOrder.getPasser_id());//乘客主键
        object.put("pinLoseAccount", passengerOrder.getLoss_money()+"");//爽约金
        httpPost(Common.HOST + Common.HOST_ROB_ORDER,object, Constant.HOST_ROB_ORDER);
    }
    @Override
    public void initData() {
        type = getIntent().getIntExtra("type",-1);
        passengerOrder = (PassengerOrder) getIntent().getSerializableExtra("passengerOrder");
        serviceId = passengerOrder.getService_id();
        ownerId = getHeadInfo().getUserId();
        passerId = passengerOrder.getPasser_id();
        pinLoseAccount = passengerOrder.getLoss_money()+"";
        tv_title.setText("订单详情");
        switch (type){
            case Constant.LONG://长途
                tv_style.setText("长途");
                text1.setText("座位需求");
                text2.setText("总价");
                break;
            case Constant.CITY://同城
                tv_style.setText("同城");
                text1.setText("座位需求");
                text2.setText("总价");
                break;
            case Constant.TRAVEL://旅游
                tv_style.setText("旅游");
                text1.setText("座位需求");
                text2.setText("总价");
                break;
            case Constant.LONG_HURRY://长途急件
                tv_style.setText("长途急件");
                text1.setText("运费");
                text2.setText("保价");
                break;
        }
        tv_distance.setText(passengerOrder.getDistance()+"公里");
        tv_depart_time.setText(passengerOrder.getDepart_time());
        tv_start_pos.setText(passengerOrder.getStart_place());
        tv_end_pos.setText(passengerOrder.getEnd_place());
        String phone = passengerOrder.getPhone();
        phone=phone.substring(0,3)+"****"+phone.substring(7,11);
        tv_phone.setText(phone);
        if (TextUtils.isEmpty(passengerOrder.getWords())){
            tv_words.setText("乘客很懒，没有留言");
        }else {
            tv_words.setText(passengerOrder.getWords());
        }
        x.image().bind(iv_icon,Common.HOST+passengerOrder.getIcon());
        tv_nickname.setText(passengerOrder.getNickname());
        tv_pay_break_money.setText("需支付爽约金"+pinLoseAccount+"元");
        if (type==Constant.LONG_HURRY){

        }else {
            text11.setText(passengerOrder.getSeats_need()+"个");
            text21.setText(passengerOrder.getTotal_price()+"元");
            text3.setText("爽约金："+passengerOrder.getLoss_money()+"元");
            tv_whos_phone.setText("乘客联系方式：");
            tv_whos_words.setText("乘客留言：");
        }
        tv_recent_come.setText("[24h前来过]");
        Picasso.with(context).load(Common.IMAGE_HOST+passengerOrder.getIcon()).error(R.mipmap.morentouxiang).transform(new CircleTransform()).into(iv_icon);
        if (TextUtils.isEmpty(passengerOrder.getNickname())){
            tv_nickname.setText("暂无昵称");

        }else {
            tv_nickname.setText(passengerOrder.getNickname());

        }

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.tv_wei:
                showToast("暂未开放");
                break;
            case R.id.tv_pay_break_money:
                break;
            case R.id.tv_rob:
                //为了保证用户点击时获取数据不为空，要么设置不可取消的加载条，要么默认按钮不可点击知道获取订单信息成功，暂时没有做处理
                if (type!=Constant.LONG_HURRY){
                    hostRobFormOrder();
                }

                break;

        }
    }

    @Override
    public void OnCenterItemClick(CenterDialog dialog, View view) {
        switch (view.getId()){
            case R.id.dialog_sure:
                switch (failure_code){
                    case Constant.NAME_VERIFYING:
                        dialog.cancel();

                        break;
                    case Constant.NAME_NOT_VERIFY:
                        startActivity(new Intent(this, MineMember.class));
                        break;
                    case Constant.NAME_VERIFY_FALSE:
                        startActivity(new Intent(this, MineMember.class));

                        break;
                    case Constant.CAR_VERIFYING:
                        dialog.cancel();

                        break;
                    case Constant.CAR_VERIFY_FALSE:
                        startActivity(new Intent(this,CarVerifyActivity.class));
                        break;
                    case Constant.CAR_NOT_VERIFY:
                        startActivity(new Intent(this,CarVerifyActivity.class));

                        break;
                    default:
                        break;
                }
                break;
            default:
                dialog.cancel();
                break;
        }
    }

    @Override
    public void onSuccess(String result, String fromWhere) {
        super.onSuccess(result, fromWhere);
        com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
        if (fromWhere.equals(Constant.HOST_ROB_ORDER)){
            JSONObject data = obj.getJSONObject("data");
            Intent intent = new Intent(PassengerOrderDetailActivity.this, BookPayActivity.class);
            if (!data.getString("orderId").equals("")){
                intent.putExtra("orderCode",data.getString("orderId"));//orderId
            }else {
                showToast("orderId为空");
                return;
            }
            intent.putExtra("amount",pinLoseAccount);//支付总金额
            intent.putExtra("from", Constant.HOST_ROB_PAY);
            intent.putExtra("id",serviceId);
            intent.putExtra("alipay",Common.HOST_SEARCH_ALIPAY);
            intent.putExtra("yivipay",Common.HOST_SEARCH_RESTPAY);
            intent.putExtra("type",Constant.HOST_ROB_OK);
            if (passengerOrder.getDepart_time()!=null){
                intent.putExtra("text_1",passengerOrder.getDepart_time());

            }else {
                showToast("出发时间为空");
                return;
            }
            intent.putExtra("text_2",data.getString("orderCode"));
            startActivity(intent);
        }

    }
}
